package com.example.demo.controller;

import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.List;

/**
 * @program: demo
 * @description: User控制层
 * @author: Zwill
 * @create: 2019-12-25 09:00
 **/

@Api(tags = "user")
@RestController
@EnableSwagger2
public class UserController {
    @Autowired
    private UserMapper userMapper;

    /*添加用户*/
    @ApiOperation("添加用户信息")
    @GetMapping("/addUser")
    public int AddUser(User user) {
        if (user != null) {
            if (user.getId() != 0) {
                //id不可以重复
                List<Integer> ids = userMapper.findAllIds();
                if (ids.contains(user.getId())) {
                    System.out.println("主键已经存在，无法插入");
                    return 0;
                } else {
                    return userMapper.insertUser(user);
                }
            }
            //人员姓名不能重复
//            if (user.getUsername() != null) {
//                List<String> usernames = userMapper.findAllUserNames();
//                if (usernames.contains(user.getUsername())) {
//                    System.out.println("该用户已被使用,添加失败！");
//                    return 0;
//                } else {
//                    return userMapper.insertUser(user);
//                }
//            }
        }
        return 0;
    }

    /*查询所有用户主键信息*/
    @ApiOperation("查询所有用户主键信息")
    @GetMapping("/findAllIds")
    public List<Integer> findAllIds() {
        return userMapper.findAllIds();
    }

    /*查询所有用户的username信息*/
//    @ApiOperation("查询所有用户名称信息")
//    @GetMapping("/findAllUserNames")
//    public List<String> findAllUserNames() {
//        return userMapper.findAllUserNames();
//    }


    /*根据id删除用户*/
    @ApiOperation("删除用户")
    @GetMapping("/deleteUser")
    public int deleteUser(int id) {
        return userMapper.deleteUser(id);
    }

    /*更新用户信息*/
    @ApiOperation("更新用户信息")
    @PostMapping("/updateUser")
    public int updateUser(User user) {
        return userMapper.updateUser(user);
    }

    /*查询所有用户*/
    @ApiOperation("查询用户")
    @GetMapping("/selectAllUsers")
    public List<User> selectAllUsers() {
        return userMapper.selectAllUsers();
    }


}
